from articles.models import Article, ArticleNote 
from django.shortcuts import render_to_response, get_object_or_404 
from django.template import RequestContext
from articles.forms import ArticleForm, ArticleNoteForm
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, Http404
from django.views.decorators.csrf import csrf_exempt, csrf_protect
from django.contrib.auth.decorators import login_required


@csrf_exempt 
def add(request):
    articleform = ArticleForm(request.POST or None)
    next = 'no'
    if articleform.is_valid():
        article = articleform.save(commit = False)
        article.user = request.user 
        article.save()
        next = 'yes'
    return render_to_response (
        'articles/add_article.html',
        {
         'articleform': articleform, 
         'next': next,
        },
        context_instance = RequestContext(request)
        ) 
  
@csrf_exempt
def update(request, article_id, dontvalidate):
    article = get_object_or_404(Article, id = article_id)
    if dontvalidate == 'yes':
        articleform = ArticleForm(instance = article)
        next = 'no'
    else:
        articleform = ArticleForm(request.POST, instance = article )
    if articleform.is_valid():
        article.save() 
        next = 'yes'
    return render_to_response(
        'articles/update_article.html',
        {
        'article_id':article_id,
        'articleform':articleform,
        'next': next,
        'dontvalidate': dontvalidate,
        },
        context_instance = RequestContext(request)
        )
  
@csrf_exempt 
def add_note(request, article_id, dontvalidate):
    saveok = 'no'
    article = get_object_or_404(Article, id = article_id)
    if dontvalidate == 'yes':
        articlenoteform = ArticleNoteForm()
    else:
        articlenoteform = ArticleNoteForm(request.POST)
    if articlenoteform.is_valid():
        note = articlenoteform.save(commit = False)
        note.article = article 
        note.save() 
        saveok = 'yes'
    article_notes = ArticleNote.objects.filter(article__user = request.user)
    return render_to_response(
        'articles/add_articlenote.html',
        {
         'dontvalidate': dontvalidate,
         'article' : article,
         'articlenoteform' : articlenoteform,
         'saveok' : saveok,
         'article_notes' : article_notes,
        },
        context_instance = RequestContext(request)
        )

@csrf_exempt
def update_note(request, note_id, dontvalidate):
    note = get_object_or_404(ArticleNote, id = note_id)
    next = 'no'
    if dontvalidate == 'yes':
        noteform = ArticleNoteForm(instance = note)
    else:
        if request.method == "POST":
            noteform = ArticleNoteForm(request.POST, instance = note )
        else:
            noteform = ArticleNoteForm(instance = note )
        if noteform.is_valid():
            note.save() 
            next = 'yes'
    return render_to_response(
        'articles/update_note.html',
        {
         'note_id':note_id,
         'noteform':noteform,
         'next': next,
         'dontvalidate': dontvalidate,
        },
        context_instance = RequestContext(request)
        )
